<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\House;
use App\Models\City;
use App\Models\HouseAttr;

use App\Models\IntermediaryModel;
use App\Services\EsService;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Faker\Factory;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class HouseController extends AdminController
{
    private $beforePassStatus;
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new House(['intermediary']), function (Grid $grid) {
            $grid->selector(function (Grid\Tools\Selector $selector) {
                $selector->selectOne('house_type', '类型', [
                    0 => '销售楼盘',
                    2 => '房屋租聘'
                ]);
            });

            $grid->column('id')->sortable();
            $grid->column('house_area');
            $grid->column('house_address');
            $grid->column('pay_money');
            $grid->column('house_status','状态')->switch();
            $grid->column('intermediary.real_name','经纪人');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            //初始化进入销售楼盘
//            $grid->model()->where('house_type',0);
            //添加功能
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $id=$actions->getKey();
                $actions->append("<a href='intermediary?id=$id'>指派经纪人</a>");
            });
            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $house_province =City::where('pid',0)->pluck('name','id');
                $filter->equal('house_province')->select($house_province)->load('house_city', '/api/city')->width(3);;
                $province_id =  request()->request->get('house_province', '');
                $house_city = City::where('pid',$province_id)->pluck('name','id');
                $filter->equal('house_city')->select($house_city)->load('house_region', '/api/city')->width(3);;
                $region_id =  request()->request->get('house_city', '');
                $house_region =  City::where('pid',$region_id)->pluck('name','id');
                $filter->equal('house_region')->select($house_region)->width(3);
            });
        });
    }
    public function intermediary(Content $content)
    {
        $id=$_GET['id'];
        return $content
            ->header('指派经济人')
            ->body($this->change($id));
    }
    protected function change($id){
       return Form::make(new House(), function (Form $form) {
           $form->hidden('id')->disable();
           $form->select('intermediary_id')->options(IntermediaryModel::get()->pluck('real_name','id'));
           })->action("change/$id")->edit($id);
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new House(), function (Show $show) {
            $show->field('id');
            $show->field('intermediary_id');
            $show->field('house_name');
            $show->field('house_area');
            $show->field('house_type');
            $show->field('house_province');
            $show->field('house_city');
            $show->field('house_region');
            $show->field('house_address');
            $show->field('longitude');
            $show->field('latitude');
            $show->field('house_direction');
            $show->field('house_build_area');
            $show->field('house_use_area');
            $show->field('house_year');
            $show->field('house_floor');
            $show->field('house_room');
            $show->field('house_img');
            $show->field('house_date_type');
            $show->field('house_price_range');
            $show->field('house_config');
            $show->field('house_label');
            $show->field('house_owner');
            $show->field('house_status');
            $show->field('house_desc');
            $show->field('house_property_type');
            $show->field('is_recommend');
            $show->field('pay_money');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }
    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new House(), function (Form $form) {
            $form->display('id');
            $form->row(function ($form){
//                $form->select('intermediary_id')->options(IntermediaryModel::get()->pluck('real_name','id'));
                $form->text('house_name');
                $form->text('house_area');
                $form->select('house_type')->options([0 => '新房', 1 => '二手房', 2 => '出租房'])->default(2);
            });
            $form->row(function ($form){
                // 省份
                $form->width(4)->select('house_province')->options(City::where('pid','')->get()->pluck('name', 'id'))->rules('required')->load('house_city', '/api/city');
                // 城市
                $form->width(4)->select('house_city')->load('house_region', '/api/city');
                // 区域
                $form->width(4)->select('house_region');
            });
            $form->row(function ($form){
                $form->text('house_address');
                $form->file('house_img')->disk('oss')->saveFullUrl();
                $form->select('house_direction')->options(HouseAttr::getHouseattrData('house_direction'));
                $form->text('house_build_area');
                $form->text('house_use_area');

                //房源楼层
                $form->select('house_floor')->options(Houseattr::getHouseattrData('house_floor'));
                //房源年代
                $form->select('house_year')->options(Houseattr::getHouseattrData('house_year'));
               //房源几室几厅几卫
                $form->select('house_room')->options(Houseattr::getHouseattrData('house_room'));
                //房源租期方式
                $form->select('house_date_type')->options(Houseattr::getHouseattrData('house_date_type'));
                //房源价格范围
                $form->select('house_price_range')->options(Houseattr::getHouseattrData('house_price_range'));
                //房源配置
                $form->multipleSelect('house_config')->options(Houseattr::getHouseattrData('house_config'));
                //房源标签
                $form->multipleSelect('house_label')->options(Houseattr::getHouseattrData('house_label'));
                $form->text('house_owner');
                $form->select('house_status')->options([0 => '待审核', 1 => '审核不通过', 2 => '审核通过']);
                $form->text('house_desc');
                $form->radio('house_property_type')->options(Houseattr::getHouseattrData('house_property_type'));
                $form->select('is_recommend')->options([0 => '否', 1 => '是']);
                $form->text('pay_money');
            });
            $form->row(function ($form){
                $form->map('latitude','longitude','map');
            });
            $form->display('created_at');
            $form->display('updated_at');
            $form->saved(function (Form $form) {
                /**
                 * 在我们添加数据保存到数据库的时候同步到我们的es中,
                 * 这个样我们可以使es里的数据与我们的数据库数据同步，方便我们的搜索使用
                 */
                $id = $form->getKey();
                $data['house_name']=$form->house_name;
                $data['house_area']=$form->house_area;
                $data['house_type']=$form->house_type;
                $data['house_province']=$form->house_province;
                $data['house_city']=$form->house_city;
                $data['house_region']=$form->house_region;
                $data['house_address']=$form->house_address;
                $data['longitude']=$form->longitude;
                $data['latitude']=$form->latitude;
                $data['house_direction']=$form->house_direction;
                $data['house_build_area']=$form->house_build_area;
                $data['house_use_area']=$form->house_use_area;
                $data['house_year']=$form->house_year;
                $data['house_floor']=$form->house_floor;
                $data['house_room']=$form->house_room;
                $data['house_img']=$form->house_img;
                $data['house_date_type']=$form->house_date_type;
                $data['house_price_range']=$form->house_price_range;
                $data['house_config']=$form->house_config;
                $data['house_label']=$form->house_label;
                $data['house_owner']=$form->house_owner;
                $data['house_status']=$form->house_status;
                $data['house_desc']=$form->house_desc;
                $data['house_property_type']=$form->house_property_type;
                $data['is_recommend']=$form->is_recommend;
                $data['pay_money']=$form->pay_money;
                $data['created_at']=$form->created_at;
                $data['updated_at']=$form->updated_at;
                $es=new EsService();
                //es入库
                $es->add_doc($id,$data);
            });
        });
    }
}
